/**
 * @file PersistenceMode.cpp
 *
 * @date 31/12/2011
 * @author Tales 
 *
 */

#include "PersistenceMode.hpp"
#include "../controller/Editor.hpp"

namespace yep
{

const string fileName = "imagem";
static const int STATE_PRESENTATION = 0, STATE_OPT_LOAD = 1, STATE_OPT_IMPORT = 2, STATE_OPT_SAVE = 3,
		STATE_OPT_EXPORT = 4, STATE_ASK = 5, STATE_CONFIRM = 6, STATE_END = -1;
pair<string, ConsoleModeBase::Function> yep::PersistenceMode::getNextLine(Editor & editor)
{
	string text;
	Function function;
	switch (state)
	{
	case STATE_PRESENTATION:
		text = "Escolha opção:";
		function = NO_FUNCTION;
		state = STATE_OPT_LOAD;
		break;
	case STATE_OPT_LOAD:
	{
		stringstream s;
		s << "1 - Carregar arquivo '" << fileName << ".lbmp'";
		text = s.str();
		function = NO_FUNCTION;
		state = STATE_OPT_IMPORT;
		break;
	}
	case STATE_OPT_IMPORT:
	{
		stringstream s;
		s << "2 - Importar arquivo '" << fileName << ".bmp'";
		text = s.str();
		function = NO_FUNCTION;
		state = STATE_OPT_SAVE;
		break;
	}
	case STATE_OPT_SAVE:
	{
		stringstream s;
		s << "3 - Salvar arquivo '" << fileName << ".lbmp'";
		text = s.str();
		function = NO_FUNCTION;
		state = STATE_OPT_EXPORT;
		break;
	}
	case STATE_OPT_EXPORT:
	{
		stringstream s;
		s << "4 - Exportar arquivo '" << fileName << ".bmp'";
		text = s.str();
		function = NO_FUNCTION;
		state = STATE_ASK;
		break;
	}
	case STATE_ASK:
		text = "Qual opção deseja selecionar? (1 - 4 = mudar; 0 = nao mudar): ";
		function = INPUT_CHAR;
		break;
	case STATE_CONFIRM:
		text = "Tem certeza que deseja realizar esta operação? (S/N): ";
		function = INPUT_YESNO;
		break;
	case STATE_END:
		text = "";
		function = END;
		break;
	}

	return make_pair(text, function);
}

void yep::PersistenceMode::handleAnswer(Editor & editor, string answer)
{
	if (state == STATE_ASK)
	{
		stringstream s(answer);
		s >> option;
		if (option == 0)
			state = STATE_END;
		else if (option > 0 && option <= 4)
		{
			--option;
			state = STATE_CONFIRM;
		}
	}
	else if (state == STATE_CONFIRM)
	{
		if (answer == "yes")
		{
			switch (option)
			{
			case 0: // Load
				editor.openPicture(fileName + ".lbmp");
				break;
			case 1: //Import
				editor.openPicture(fileName + ".bmp");
				break;
			case 2: // Save
				editor.savePicture(fileName + ".lbmp");
				break;
			case 3: // Export
				editor.savePicture(fileName + ".bmp");
				break;
			default:
				break;
			}
		}
		state = STATE_END;
	}
}

void yep::PersistenceMode::setup()
{
	ConsoleModeBase::setup();
	state = STATE_PRESENTATION;
}
}

/* namespace yep */
